package controllers

import (
	"app/app/services"
	"app/dao"
	"app/models"

	"github.com/kataras/iris/v12"
)

type PartnerController struct {
	Ctx iris.Context
}

func (c *PartnerController) GetFetch() *models.JsonResult {
	userid, roleid, _, err := GetUser(c.Ctx)
	if err != nil {
		c.Ctx.StatusCode(401)
		return models.JsonErrorCode(401, "请重新登录")
	}
	var data []dao.Partner
	switch roleid {
	case 1:
		data = services.PartnerService.FetchByAdmin()
	case 2:
		data = services.PartnerService.FetchByLeader(userid)
	case 3:
		data = services.PartnerService.FetchByUser(userid)
	}
	return models.JsonData(data)
}

func (c *PartnerController) PostCreate() *models.JsonResult {
	partnerModel := &dao.Partner{}
	if err := c.Ctx.ReadJSON(partnerModel); err != nil {
		return models.JsonErrorCode(501, err.Error())
	}
	partnerModel.Createuserid = GetUserId(c.Ctx)
	err := services.PartnerService.Insert(*partnerModel)
	if err != nil {
		return models.JsonErrorCode(501, err.Error())
	} else {
		return models.JsonData("success")
	}
}
func (c *PartnerController) PostUpdate() *models.JsonResult {
	partnerModel := &dao.Partner{}
	if err := c.Ctx.ReadJSON(partnerModel); err != nil {
		return models.JsonErrorCode(501, err.Error())
	}
	err := services.PartnerService.Update(*partnerModel)
	if err != nil {
		return models.JsonErrorCode(501, err.Error())
	} else {
		return models.JsonData("success")
	}
}
